#!/bin/bash

# 加载环境变量
. .env.sh

function deduplication(){
  echo $* | sed 's/ /\n/g' | sort -u -r 
}

function msg(){
  INDEX=0
  echo -e "\e[32m [ ${1} ${2} `
  for i in $*
  do
   if [ ${INDEX} -lt 2 ];then
    INDEX=$[ ${INDEX} +1 ]
    continue
   fi
   echo -n $i" "
  done
  `]\e[00m"
}

function appctl(){
  case $1 in 
  start)
  # 启动选项
    case $2 in 
      api)
        if [ `([ -f api.pid ] &&  ps -p $(cat api.pid) &> /dev/null && echo 0 ) || echo 1` -eq 1 ];then
          cd gtlvm-api/
            nohup python3 run.py &> ./logs.log &
            echo $! > ../api.pid
            msg api start ok pid: $(cat ../api.pid)
          cd ..
          return 0
        else
          msg api 服务已经启动，请检查 $(cat api.pid)
        fi
      ;;
      scrcpy)
        if [ `([ -f scrcpy.pid ] && ps -p $(cat scrcpy.pid) &> /dev/null && echo 0 ) || echo 1` -eq 1 ];then
          cd ws-scrcpy
            nohup node dist/index.js &> ./logs.log &
            echo $! > ../scrcpy.pid
            msg scrcpy start ok pid: $(cat ../scrcpy.pid)
          cd ..
          return 0
        else
          msg scrcpy 服务已经启动,检查scrcpy.pid文件 $(cat ../scrcpy.pid)
        fi
      ;;
      nginx)
        if [ -d "nginx" ];then
          cd nginx
              if [ ! -f ./logs/nginx.pid ] ;then
                ./sbin/nginx 
                msg nginx start ok pid: $(cat ./logs/nginx.pid)
              else
                msg nginx 已经存在,请检查nginx/logs/nginx.pid文件 
              fi
          cd ..
          return 0
        fi

       
      ;;
    esac
  ;;
  stop)
  # 关闭选项
    case $2 in 
      api)
        if [ -f api.pid ] ;then
          API_PID=$(deduplication `ps -elf |awk '{print $4" " $5}' |grep "$(cat api.pid)\$"`  `cat api.pid` )
          kill   ${API_PID}
          rm api.pid
          msg api stop ok pid: ${API_PID}
          return 0
        else
          msg scrcpy 'notStart'
        fi
      ;; 
      scrcpy)
        if [ -f scrcpy.pid ];then
          SCRCPY_PIDS=$(deduplication `ps -elf | awk '{print $4" "$5}' |grep "$(cat scrcpy.pid)\$"` `cat scrcpy.pid` )
          kill   ${SCRCPY_PIDS}
          rm scrcpy.pid
          msg scrcpy stop ok pid: ${SCRCPY_PIDS}
          return 0
        else
          msg scrcpy 'notStart'
        fi
      ;;
      nginx)
        if [ -d "nginx" ];then
          cd nginx
            if [ -f ./logs/nginx.pid ] ;then
              NGINX_PID=$(cat ./logs/nginx.pid )
              ./sbin/nginx -s stop
              [ -f ./logs/nginx.pid ] && rm ./logs/nginx.pid 
              msg nginx stop ok pid: ${NGINX_PID}
            else
              msg nginx 'notStart'
            fi
          cd ..
          return 0
        fi
    esac
  ;;
  *)
    echo "参数错误"
    return 1
  ;;
  esac
}
case $1 in
start)
    appctl start api
    appctl start scrcpy
    appctl start nginx
    exit
  ;;
esac
case $1 in
stop)
    appctl stop api
    appctl stop scrcpy
    appctl stop nginx
    exit
  ;;
esac
case $1 in
restart)
    appctl stop api
    [ $? -eq 0 ] || exit
    appctl stop scrcpy
    [ $? -eq 0 ] || exit
    appctl stop nginx
    [ $? -eq 0 ] || exit
    appctl start api
    [ $? -eq 0 ] || exit
    appctl start scrcpy
    [ $? -eq 0 ] || exit
    appctl start nginx
    [ $? -eq 0 ] || exit
    exit
  ;;
api)
  appctl $2 api 
  exit
;;

scrcpy)
  appctl $2 scrcpy 
  exit
;;

nginx)
  appctl $2 nginx 
  exit
;;

env)
  echo "load env"
;;
*)
echo -e '\n请输入参数!
  command: start.sh [start|stop|restart|${option}]
    option:
      api [start|stop]
      nginx [start|stop]
      scrcpy [start|stop]
  '
;;
esac

